<?php
/**
 * WQPHP封装工具类，仅供学习研究使用，禁止任何形式的转载发布
 * User: Jessica@aweb.wang
 * Date: 2023/3/10 14:50
 * Email: <1194323013@qq.com>
 *                            _ooOoo_
 *                           o8888888o
 *                           88" . "88
 *                           (| -_- |)
 *                           O\  =  /O
 *                        ____/`---'\____
 *                      .'  \\|     |//  `.
 *                     /  \\|||  :  |||//  \
 *                    /  _||||| -:- |||||-  \
 *                    |   | \\\  -  /// |   |
 *                    | \_|  ''\---/''  |   |
 *                    \  .-\__  `-`  ___/-. /
 *                  ___`. .'  /--.--\  `. . __
 *               ."" '<  `.___\_<|>_/___.'  >'"".
 *              | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 *              \  \ `-.   \_ __\ /__ _/   .-` /  /
 *         ======`-.____`-.___\_____/___.-`____.-'======
 *                            `=---='
 *        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 *                      Buddha Bless, No Bug !
 **/

namespace app\common\utils;
use app\common\exception\BusinessException;
use app\common\exception\BusinessExceptionEnum;
use PHPExcel_IOFactory;
use think\facade\Filesystem;

class ExcelUtils
{
    public static function shibie($excelFile){
        $retData=UploadUtils::excelupload($excelFile,'/excel');
        $inputFileName=$retData['sendurl'];
        $inputFileType=PHPExcel_IOFactory::identify($inputFileName);
        if($retData['ext']=='xlsx'){
            $objReader=\PHPExcel_IOFactory::createReader('Excel2007');
        }else if($retData['ext']=='xls'){
            $objReader=\PHPExcel_IOFactory::createReader('Excel5');
        }

        $objPHPExcel=$objReader->load($inputFileName);

        $data['excelArray']=$objPHPExcel->getSheet(0)->toArray();
        $data['highestRow']=$objPHPExcel->getSheet(0)->getHighestRow()-1;
        unlink($retData['sendurl']);
        return $data;
    }
    public static function makeExcel($data,$filename,$title,$description,$keywords,$category){
        //初始化
        $filename=str_replace('.xls', '', $filename).'.xls';
        $phpexcel = new \PHPExcel();
        $phpexcel->getProperties()
            ->setCreator("CMSSYSTEM")
            ->setLastModifiedBy("CMSSYSTEM")
            ->setTitle($title)
            ->setSubject($title)
            ->setDescription($description)
            ->setKeywords($keywords)
            ->setCategory($category);
        $phpexcel->getActiveSheet()->fromArray($data);
        $phpexcel->getActiveSheet()->setTitle('Sheet1');
        $phpexcel->setActiveSheetIndex(0);
        header('Content-Type: application/vnd.ms-excel');
        header("Content-Disposition: attachment;filename=$filename");
        header('Cache-Control: max-age=0');
        header('Cache-Control: max-age=1');
        header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
        header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
        header ('Pragma: public'); // HTTP/1.0
        $objwriter = PHPExcel_IOFactory::createWriter($phpexcel, 'Excel5');
        $objwriter->save('php://output');
        exit;
    }
}